package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.adapter.Fill_Information2_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Information;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GeRen_Update_Msg_Sflb_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private LinearLayout ll_geren_main_update_work;
    private String access_token, work;
    private TextView tv_feedback_tijiao;
    private MyGridView gv_fill_item_type;
    private User_Information user_information;
    private Fill_Information2_Adapter fill_information2_adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren_update_msg_sflb);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        user_information = (User_Information) intent.getSerializableExtra("user_information");
        back = findViewById(R.id.back);
        gv_fill_item_type = findViewById(R.id.gv_fill_item_type);
        tv_feedback_tijiao = findViewById(R.id.tv_feedback_tijiao);
        ll_geren_main_update_work = findViewById(R.id.ll_geren_main_update_work);
        back.setOnClickListener(this);
        tv_feedback_tijiao.setOnClickListener(this);
        ll_geren_main_update_work.setOnClickListener(this);
        gv_fill_item_type.setVerticalSpacing(15);
        gv_fill_item_type.setHorizontalSpacing(15);
        SharedPreferences preferences = getSharedPreferences("update_message", Context.MODE_PRIVATE);
        String set_work = preferences.getString("identity" ,"");
        if (! ToolUtils.isEmpty(set_work)){
            if (set_work.equals("不显示")){
                ll_geren_main_update_work.setBackgroundResource(R.drawable.login_login);
            } else {
                ll_geren_main_update_work.setBackgroundResource(R.drawable.login_exit);
            }
        } else {
            ll_geren_main_update_work.setBackgroundResource(R.drawable.login_exit);
        }
        fill_information2_adapter = new Fill_Information2_Adapter(this, user_information, 7);
        fill_information2_adapter.setName(set_work);
        work = set_work;
        gv_fill_item_type.setAdapter(fill_information2_adapter);
        gv_fill_item_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                fill_information2_adapter.setName("");
                ToolUtils.showToast(GeRen_Update_Msg_Sflb_Activity.this, user_information.getIdentity().get(position));
                work = user_information.getIdentity().get(position);
                ll_geren_main_update_work.setBackgroundResource(R.drawable.login_exit);
                fill_information2_adapter.setSelect(position);
                fill_information2_adapter.notifyDataSetChanged();
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.ll_geren_main_update_work:
                work = "不显示";
                fill_information2_adapter.setName("");
                fill_information2_adapter.setSelect(-1);
                fill_information2_adapter.notifyDataSetChanged();
                ll_geren_main_update_work.setBackgroundResource(R.drawable.login_login);
                break;
            case R.id.tv_feedback_tijiao:
                SharedPreferences preferences = getSharedPreferences("update_message", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("identity", work);
                editor.apply();
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_xyf_Url + access_token + "&limit=10", null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
//                        lv_xyf_jilu.setAdapter();
                    } else {
                        ToolUtils.showToast(GeRen_Update_Msg_Sflb_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(GeRen_Update_Msg_Sflb_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
